package cn.wolfcode.luowowo.job;

import cn.wolfcode.luowowo.redis.service.IStrategyStatiesVORedisService;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import cn.wolfcode.luowowo.redis.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 将Redis中的数据 定时 的持久化到数据库中
 */
@Component
public class RedisDataPersistenceJob {

    @Autowired
    private IStrategyStatiesVORedisService strategyStatiesVORedisService;

    @Autowired
    private IStrategyService strategyService;


    //定时任务标签：cron制定任务计划，什么时候执行该方法，周期是多少
    @Scheduled(cron = "0/20 * * * * ? ")
    public void doWork() {
        System.out.println("__________vo持久化开始__________");
        //1.从Redis中获取所有的攻略vo的数据
        String pattern = RedisKeys.STRATEGY_STATIS_VO.join("*");//匹配所有前缀一样的vo统计数据
        List<StrategyStatisVO> list = strategyStatiesVORedisService.listStrategyVoByPattern(pattern);

        //2.将获取到的vo数据，vo中的数据全部set给攻略对象，保存到mongodb中
        for (StrategyStatisVO vo : list) {
            strategyService.updateStrategy(vo);
        }

        System.out.println("__________vo持久化结束__________");

    }

}
